#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    int numberOfWeakCharacters(vector<vector<int>>& properties) {
        sort(properties.begin(), properties.end(), [&](const vector<int>& a, const vector<int>& b)
        {
            if(a[0] != b[0])
                return a[0] < b[0];
            else
                return a[1] > b[1];
        });
        stack<int> st;
        int ans = 0;
        for(auto& i : properties)
        {
            while(!st.empty() && st.top() < i[1])
            {
                ans ++;
                st.pop();
            }
            st.push(i[1]);
        }
        return ans;
    }
};